package GUILayer;
import java.awt.Dimension;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;

import CtrlLayer.ItemCtrl;
import CtrlLayer.OrderCtrl;
import ModelLayer.SaleItem;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

@SuppressWarnings("serial")
public class PopupTilfoejVare extends PopupWindow
{
	private JTextField txtItemnr;
	private JTextField txtAmount;
	private ItemCtrl ic;
	private JTextField txtOrdernr;
	private OrderCtrl orderCtrl;
	
	public PopupTilfoejVare(Updateable mother)
	{
		super(mother);
	    ic = new ItemCtrl();
		
		getContentPane().setPreferredSize(new Dimension(400, 100));
		setTitle("Tilfoej vare");
		
		JPanel panel = new JPanel();
		
		JLabel label = new JLabel("Antal:");
		
		JLabel label_1 = new JLabel("Varenr.:");
		
		txtItemnr = new JTextField();
		txtItemnr.setColumns(10);
		
		txtAmount = new JTextField();
		txtAmount.setColumns(10);
		
		JButton button = new JButton("Tilfoej vare");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				addPartOrder();
				clearFields();
				getMother().update();
				quit();
			}
		});
		
		txtOrdernr = new JTextField();
		txtOrdernr.setVisible(false);
		txtOrdernr.setEditable(false);
		txtOrdernr.setColumns(10);
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addContainerGap()
							.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
								.addComponent(label, Alignment.TRAILING)
								.addComponent(label_1, Alignment.TRAILING))
							.addGap(10)
							.addGroup(gl_panel.createParallelGroup(Alignment.LEADING, false)
								.addComponent(txtItemnr, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 170, Short.MAX_VALUE)
								.addComponent(txtAmount, Alignment.TRAILING))
							.addGap(19)
							.addComponent(button))
						.addComponent(txtOrdernr, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(44, Short.MAX_VALUE))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_panel.createSequentialGroup()
					.addComponent(txtOrdernr, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtItemnr, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(label_1))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(label)
						.addComponent(txtAmount, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(button))
					.addContainerGap())
		);
		panel.setLayout(gl_panel);
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(panel, GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
					.addContainerGap())
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 76, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(313, Short.MAX_VALUE))
		);
		getContentPane().setLayout(groupLayout);
		pack();
		
		orderCtrl = new OrderCtrl();
	}
	
	public void addPartOrder()
	{
		int ordernr = Integer.parseInt(txtOrdernr.getText());
		int amount = Integer.parseInt(txtAmount.getText());
		int itemnr = Integer.parseInt(txtItemnr.getText());
		SaleItem saleitem = ic.searchSaleItem(itemnr);
		ModelLayer.RentItem rentitem = null;
		double subTotal = 0;
		int quantityDiscount = 0;
		
		orderCtrl.addPartOrder(ordernr, amount, saleitem, rentitem, subTotal, quantityDiscount);
	}
	
	public void addnr()
	{
		int ordernr = orderCtrl.getLOrder();
        txtOrdernr.setText("" + ordernr);
	}
	
	public void clearFields()
	{
		txtItemnr.setText(null);
		txtAmount.setText(null);
	}
}
